package controller;

import com.github.pagehelper.PageInfo;
import entity.Customer;
import entity.LayRequest;
import entity.MyResult;
import entity.SaleOpportunity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import service.CustomerService;
import service.SaleService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lay
 * @date 2020/6/18 9:16
 */
@Controller
@RequestMapping("/sale")
public class SaleController {

    @Autowired
    private SaleService saleService;

    @Autowired
    private CustomerService customerService;

    /**
     * layui添加功能
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd(HttpSession httpSession){
        List<Customer> customers = customerService.selectAll();
        httpSession.setAttribute("customers",customers);
        return "add.jsp";
    }
    @ResponseBody
    @RequestMapping("/add")
    public MyResult add(SaleOpportunity saleOpportunity){
        saleService.insert(saleOpportunity);
        return new MyResult<>(0);
    }
    /**
     * layui修改功能
     * @param id
     * @param modelMap
     * @return
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id,ModelMap modelMap,HttpSession httpSession){
        SaleOpportunity saleOpportunity = saleService.selectById(id);
        modelMap.addAttribute("saleOpportunity",saleOpportunity);
        List<Customer> customers = customerService.selectAll();
        httpSession.setAttribute("customers",customers);
        return "update.jsp";
    }
    @ResponseBody
    @RequestMapping("/update")
    public MyResult update(SaleOpportunity saleOpportunity){
        saleService.update(saleOpportunity);
        return new MyResult<>(0);
    }
    /**
     * layui详情页面
     * @param id
     * @param modelMap
     * @return
     */
    @RequestMapping("/details")
    public String details(Integer id,ModelMap modelMap,HttpSession httpSession){
        SaleOpportunity saleOpportunity = saleService.selectById(id);
        modelMap.addAttribute("saleOpportunity",saleOpportunity);
        List<Customer> customers = customerService.selectAll();
        httpSession.setAttribute("customers",customers);
        return "details.jsp";
    }
    /**
     * 删除功能
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public MyResult delete(Integer id){
        saleService.delete(id);
        return new MyResult(200,"删除成功!");
    }

    /**
     * layui批量删除功能
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("/batchDelete")
    public MyResult batchDelete(Integer[] ids){
        System.out.println(Arrays.toString(ids));
        saleService.batchDelete(ids);
        return new MyResult<>(0);
    }

    /**
     * 列表页面
     * @param modelMap
     * @return
     */
    @RequestMapping("/saleList")
    public String saleList(ModelMap modelMap, @RequestParam Map<String,Object> map){
        //执行分页语句
        PageInfo<SaleOpportunity> saleOpportunityPageInfo = saleService.selectByPage(map);
        //将分页查询结果传递到页面
        modelMap.addAttribute("saleOpportunities",saleOpportunityPageInfo);
        //回传查询条件
        modelMap.addAttribute("params",map);
        return "saleList.jsp";
    }

    /**
     * layui列表页面
     * @param layRequest
     * @return
     */
    @ResponseBody
    @RequestMapping("/list")
    public MyResult<List<SaleOpportunity>> list(LayRequest layRequest, HttpSession httpSession){
        List<Customer> customers = customerService.selectAll();
        httpSession.setAttribute("customers",customers);
        //执行分页语句
        PageInfo<SaleOpportunity> saleOpportunityPageInfo = saleService.selectByPage(layRequest);
        //将分页查询结果传递到页面
        return new MyResult<>(0,saleOpportunityPageInfo.getTotal(),saleOpportunityPageInfo.getList());
    }

    /**
     * 富文本框图片上传
     * @param file
     * @param request
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/uploadImg")
    public MyResult<Map> uploadImg(MultipartFile file, HttpServletRequest request) throws IOException {
        String filename = file.getOriginalFilename();
        System.out.println("上传文件名："+filename);
        File f = new File("E:/articleImg/" + filename);
        if(!f.getParentFile().exists()){
            f.getParentFile().mkdirs();//如果父类目录不存在，创建该目录
        }
        //保存文件，将上传的文件写入file
        file.transferTo(f);
        Map map = new HashMap<>();
        map.put("src",request.getContextPath()+"/articleImg/"+filename);//图片访问路径
        map.put("title",filename);//图片标题

        return new MyResult<>(0,"",map);
    }

}
